﻿@using System.Globalization
@{
    ViewBag.Title = "模型字段配置";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .add
        {
            display: none;
            width: 200px;
        }
        .addColumn select
        {
            width: 200px;
        }
        .td1
        {
            width: 100px;
            text-align: right;
        }
        #editColumn
        {
            display: none;
        }
        .divGrid
        {
            max-height: 480px;
            overflow-y: scroll;
        }
        .version, .modelEdit
        {
            display: none;
        }
    </style>
}
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var retData = new Array();
    var inColumn = new Array();
    inColumn.push("mdp_guid");
    inColumn.push("mdp_batchtime");
    inColumn.push("mdp_isdelete");
    inColumn.push("mdp_operationtype");
    var opts = {
        height: 380,
        checkbox: true,
        rows: 500,
        url: "/ModelConfig/GetModelColumn",
        para: { modelID: UrlArray["modelID"], version: UrlArray["version"], dbName: encodeURI(UrlArray["dbName"]) },
        idField: "GUID",
        columns: [
                { field: 'ColumnName', title: '字段名称', width: 20 },
                { field: 'ColumnDescription', title: '字段描述', width: 20 },
                { field: 'ColumnType', title: '字段类型', width: 13 },
                { field: 'PK', title: '主键', width: 7, align: 'center' },
                { field: 'SourceSysName', title: '源系统', width: 10, align: 'center' },
                { field: 'TableName', title: '表名称', width: 15, align: 'center' },
                { field: 'FactColumnName', title: '表字段', width: 15, formatter: function (value, rowData, rowIndex) {
                    var add = true; ;
                    for (var j = 0; j < retData.length; j++) {
                        if (retData[j].GUID == rowData.GUID) {
                            add = false;
                        }
                    }
                    if (add) {
                        retData.push(rowData);
                    }
                    return value;
                }
                }
                ],
        OnChickTR: function (value, rowData, rowIndex) {
            if (!UrlArray["version"]) {
                UpdateModel($.trim(rowIndex), "");
            }
        }
    };
    var urlPara;
    $(function () {
        urlPara = "?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"])
        + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
        if (UrlArray["version"]) {
            $(".version").show();
            urlPara = urlPara + "&version=" + UrlArray["version"];
            opts.checkbox = false;
        } else
            $(".modelEdit").show();
        $('#DataGrid').dg(opts);
        $("#link a").each(function () {
            $(this).attr("href", $(this).attr("href") + urlPara);
        })
    });
    function AutoBindData() {
        opts.data = null;
        retData = new Array();
        opts.url = "/ModelConfig/GetSelectTableInfo";
        $('#DataGrid').dg(opts);
        $('#editColumn').hide();
    }
    function UpdateModel(v, t) {
        EditColumn(t, v);
    }
    function returnData(v) {
        if (v.GUID == "") {
            v.GUID = retData.length;
        }
        retData[v.GUID] = v;
        var data = { "total": v.length, "rows": retData };
        opts["data"] = data;
        $("#DataGrid").dg(opts);
    }
    function DeleteRow() {
        if (confirm("确定要删除吗?")) {
            var pass = false;
            $('#DataGrid').find(":checkbox").each(function (i, d) {
                if (i == 0)
                    return;
                if (d.checked) {
                    pass = true;
                }
            });
            if (!pass) {
                alert("请选择要删除的行！");
                return;
            }
            var j = 1;
            $('#DataGrid').find(":checkbox").each(function (i, d) {
                if (i == 0)
                    return;
                if (d.checked) {
                    retData.removeAt(i - j);
                    j++;
                }
            });
            var data = { "total": 1, "rows": retData };
            opts["data"] = data;
            $("#DataGrid").dg(opts);
        }
    }
    function save(v) {
        $("#msg").html("");
        if (retData.length == 0) {
            alert("模型字段不能为空，请添加模型字段！");
            return;
        }
        var extpk = false;
        for (var i = 0; i < retData.length; i++) {
            if (retData[i].PK == "是") {
                extpk = true;
                break;
            }
        }
        if (!extpk) {
            alert("缺少主键，请设置模型主键！");
            return;
        }
        for (var i = 0; i < retData.length; i++) {
            if (inColumn.indexOf(retData[i].ColumnName.toLowerCase()) > -1) {
                alert(retData[i].ColumnName + "是系统字段，请使用其他字段名称！");
                return;
            }
        }
        $("#loading").show();
        var modelID = UrlArray["modelID"];
        var dbName = UrlArray["dbName"];
        if (UrlArray["nmID"] && UrlArray["nmID"] != "null") {
            modelID = UrlArray["nmID"];
            dbName = UrlArray["ndbName"];
        }
        $.post("/ModelConfig/SaveModelColumn", { modelID: modelID, data: $.toJSON(retData).replaceall("<br />&nbsp;", "&"), dbName: encodeURI(dbName) }, function (result) {
            if (result == "1") {
                if (v == "next")
                    goto();
                else
                    alert("操作成功！");
            }
            else
                $("#msg").html(result);

            $("#loading").hide();
        });
    } function back() {
        window.location = "/DQConfig/Index" + urlPara;
    } function goto() {
        window.location = "/ModelConfig/ExtendEdit" + urlPara;
    }
    function gotoUrl(v) {
        window.location = v + urlPara;
    }
</script>
<script type="text/javascript">
    var sysArr = new Array();
    var viewArr = new Array();
    var selID = "";
    function EditColumn(tp, id) {
        sysArr = new Array();
        viewArr = new Array();
        jv.SetDivEmpty(".searchArea", 4); //设置属性空值 
        $("#editColumn").show();
        $("#txt_ColumnType").val("-请选择-");
        $("#PK").removeAttr("checked");
        $(".addColumn div:not(:first)").remove();
        $("#sel_SourceSysName0").empty();
        $("#sel_ViewName0").empty();
        $("#sel_DisplayName0").empty();
        var sn;
        var vn;
        var dn;
        if (tp == "add") {
            selID = ""
            $(".add").show();
            $(".edit").hide();
        } else if (id) {
            selID = id;
            var i = id;
            $("#txt_ColumnName").val(retData[i].ColumnName);
            $("#txt_ColumnDescription").val(retData[i].ColumnDescription);
            $("#txt_ColumnType").val(retData[i].ColumnType);
            if (retData[i].PK == "是")
                $("#PK").attr("checked", "checked");
            sn = retData[i].SourceSysName.split("<br />&nbsp;");
            vn = retData[i].TableName.split("<br />&nbsp;");
            dn = retData[i].FactColumnName.split("<br />&nbsp;");
        }
        $.post("/ODSConfig/GetStging_dbInfo/?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]), function (result) {
            var initSourceSysName = "";
            var initViewName = "";
            for (var i = 0; i < result.rows.length; i++) {
                if (sysArr.indexOf(result.rows[i].SourceSysName) == -1) {
                    sysArr.push(result.rows[i].SourceSysName);
                    if (sn && sn[0] == result.rows[i].SourceSysName) {
                        $("#sel_SourceSysName0").append("<option selected>" + result.rows[i].SourceSysName + "</option>");
                        initSourceSysName = result.rows[i].SourceSysName;
                    } else
                        $("#sel_SourceSysName0").append("<option value='" + result.rows[i].SourceSysName + "'>" + result.rows[i].SourceSysName + "</option>");
                }
                if ((sn && sn[0] == result.rows[i].SourceSysName) || (!sn && result.rows[0].SourceSysName == result.rows[i].SourceSysName)) {
                    if (vn && vn[0] == result.rows[i].DisplayName) {
                        $("#sel_ViewName0").append("<option value='" + result.rows[i].ViewName + "' selected>" + result.rows[i].DisplayName + "</option>");
                        initViewName = result.rows[i].ViewName;
                    } else {
                        $("#sel_ViewName0").append("<option value='" + result.rows[i].ViewName + "'>" + result.rows[i].DisplayName + "</option>");
                        if (initViewName == "")
                            initViewName = result.rows[i].ViewName;
                    }
                }
                var va = { ViewName: result.rows[i].ViewName, GUID: result.rows[i].GUID, SourceSysName: result.rows[i].SourceSysName, DisplayName: result.rows[i].DisplayName };
                viewArr.push(va);
            }
            if (dn)
                GetColList(initViewName, "0", dn[0]);
            else
                GetColList(initViewName, "0", '');
            if (sn) {
                for (var i = 1; i < sn.length; i++) {
                    AddColumn(sn[i], vn[i], dn[i]);
                }
            }
        });
        $("#ColumnType").empty();
        $("#ColumnType").append("<option value='-请选择-'>-请选择-</option>");
        var extColType = "binary,varbinary,geography,geometry,hierarchyid,image,text,ntext,xml,timestamp";
        var arrColType = extColType.split(",");
        $.post("/ModelConfig/GetALLColumnTypeList", function (result) {
            var tp = new Array();
            for (var i = 0; i < result.length; i++) {
                if (arrColType.indexOf(result[i].ColumnType) == -1) {
                    tp.push("<option value='" + result[i].ColumnType + "'>" + result[i].ColumnType + "</option>");
                }
            }
            $("#ColumnType").append(tp.join(""));
        });
    }
    var ci = 0;
    function AddColumn(s, t, f) {
        ci++;
        var sci = String(ci);
        var html = new Array();
        var initViewName = "";
        html.push("<div class='col" + sci + "'><select id='sel_SourceSysName" + sci + "' class='sys' onchange='GetViewList(this.value, \"" + sci + "\")'>");
        if (s == "") {
            html.push("<option value=''>-请选择源系统-</option>");
        }
        for (var i = 0; i < sysArr.length; i++) {
            if (sysArr[i] == s)
                html.push("<option value='" + sysArr[i] + "' selected>" + sysArr[i] + "</option>");
            else
                html.push("<option value='" + sysArr[i] + "'>" + sysArr[i] + "</option>");
        }
        html.push("</select> _<select id='sel_ViewName" + sci + "' class='view' onchange='GetColList(this.value, \"" + sci + "\",\"\")'>");

        for (var i in viewArr) {
            if (s == viewArr[i].SourceSysName) {
                if (viewArr[i].DisplayName == t) {
                    html.push("<option value='" + viewArr[i].ViewName + "' selected>" + viewArr[i].DisplayName + "</option>");
                    initViewName = viewArr[i].ViewName;
                } else {
                    html.push("<option value='" + viewArr[i].ViewName + "'>" + viewArr[i].DisplayName + "</option>");
                    if (initViewName == "")
                        initViewName = viewArr[i].ViewName;
                }
            }
        }
        html.push("</select> _ <select id='sel_DisplayName" + sci + "' class='dis'></select> <input type='button' class='button' value='删除' onclick='DelColumn(" + sci + ")' /></div>");
        $(".addColumn").append(html.join(""));
        if (initViewName != "")
            GetColList(initViewName, ci, f);
    }
    function DelColumn(i) {
        $(".col" + i).remove();
    }
    function GetViewList(v, j) {
        $("#sel_ViewName" + j).empty();
        $("#sel_DisplayName" + j).empty();
        $("#sel_ViewName" + j).append("<option value=''>-请选择-</option>");
        for (var i = 0; i < viewArr.length; i++) {
            if (viewArr[i].SourceSysName == v) {
                $("#sel_ViewName" + j).append("<option value='" + viewArr[i].ViewName + "'>" + viewArr[i].DisplayName + "</option>");
            }
        }
    }
    function GetColList(v, j, f) {
        $("#sel_DisplayName" + j).empty();
        $.post('/ODSConfig/GetTableInfo?tableName=' + v + '&dbName=' + encodeURI(UrlArray["dbName"]), function (result) {
            for (var i = 0; i < result.rows.length; i++) {
                if (result.rows[i].ColumnName == f)
                    $("#sel_DisplayName" + j).append("<option value='" + result.rows[i].ColumnSourceID + "' selected>" + result.rows[i].ColumnName + "</option>");
                else
                    $("#sel_DisplayName" + j).append("<option value='" + result.rows[i].ColumnSourceID + "'>" + result.rows[i].ColumnName + "</option>");
            }
        });
    }
    function saveEdit() {
        var columnName = $.trim($("#txt_ColumnName").val());
        if (columnName == "") {
            alert("模型字段不能为空，请添加模型字段！");
            return;
        }
        else if (inColumn.indexOf(columnName.toLowerCase()) > -1) {
            alert(columnName + "是系统字段，请使用其他字段名称！");
            return;
        }
        if ($.trim($("#txt_ColumnType").val()) == "" || $.trim($("#txt_ColumnType").val()) == "-请选择-") {
            alert("模型字段类型不能为空，请添加模型字段类型！");
            return;
        } else {
            var extColType = "binary,varbinary,geography,geometry,hierarchyid,image,text,ntext,xml,timestamp";
            var arrColType = extColType.split(",");
            if (arrColType.indexOf($.trim($("#txt_ColumnType").val())) > -1) {
                alert("模型字段类型不可用，请选择其他字段类型！");
                return;
            }
        }
        for (var i = 0; i < retData.length; i++) {
            if (retData[i].ColumnName.toLowerCase() == $.trim($("#txt_ColumnName").val()).toLowerCase() && i != selID) {
                alert("该模型字段名称已存在，请使用其它名称！");
                return;
            }
        }
        var retArr = {};
        retArr["GUID"] = selID;
        retArr["ColumnDescription"] = $.trim($("#txt_ColumnDescription").val());
        retArr["ColumnType"] = $.trim($("#txt_ColumnType").val());
        if ($("#PK")[0].checked)
            retArr["PK"] = "是";
        else
            retArr["PK"] = "否";
        retArr["ColumnName"] = $.trim($("#txt_ColumnName").val());
        retArr["SourceSysName"] = getArrValue(".sys", "<br />&nbsp;");
        retArr["TableName"] = getArrValue(".view", "<br />&nbsp;", "text");
        retArr["FactColumnName"] = getArrValue(".dis", "<br />&nbsp;", "text");
        retArr["ColumnSourceID"] = getArrValue(".dis", "<br />&nbsp;");

        $("#editColumn").hide();
        returnData(retArr);
        return false;
    }
    function getColumnType(v) {
        $("#txt_ColumnType").val(v);
    }
</script>
<div style="margin: 5px;">
    <table class="listTitle" id="link">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="600">
                <div class="version">
                    <a href="/ModelConfig/Index">模型管理</a> --> <a href="/ModelConfig/Edit">查看模型渐变维度</a>
                    --> 模型字段配置
                </div>
                <div class="modelEdit">
                    <a href="/ModelConfig/Index">模型管理</a> --> <a href="/ModelConfig/Edit">新增模型配置</a>
                    --> <a href="/ODSConfig/ODSConfigAdd">选择源系统表</a> --> <a href="/DQConfig/Index">数据质量配置列表</a>
                    --> 模型字段配置
                </div>
            </td>
            <td align="right">
                <div class="version">
                    <input type="button" class="button" value="查看模型扩展字段" onclick="gotoUrl('/ModelConfig/ExtendEdit')"
                        style="width: 125px;" />
                    <input type="button" class="button" value="返回" onclick="gotoUrl('/ModelConfig/ConfigRecordList')" />
                </div>
                <div class="modelEdit">
                    <input type="button" class="button" value=" 添加行 " onclick="UpdateModel('', 'add');" />
                    <input type="button" class="button" value=" 删除行 " onclick="DeleteRow()" />
                    <input type="button" class="button" value=" 自动填充 " onclick="AutoBindData()" />
                </div>
            </td>
        </tr>
    </table>
    <div class="divGrid">
        <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
        </table>
    </div>
    <div id="msg">
    </div>
    <div class="modelEdit">
        <input type="button" class="button" value="上一步" onclick="back()" />
        <input type="button" class="button" value="保存" onclick="save()" />
        <input type="button" class="button" value="下一步" onclick="save('next')" />
    </div>
    <div id="loading" class="loading">
        <img src="/Images/tree_loading.gif" />
    </div>
    <div id="editColumn">
        <table class="listTitle">
            <tr>
                <td width="15">
                    <img src="/images/edit.png" />
                </td>
                <td width="600">
                    模型字段配置 -> 编辑字段信息
                </td>
                <td align="right">
                    <input type="button" class="button" value="完成" onclick="saveEdit()" />
                </td>
            </tr>
        </table>
        <table class="searchArea" width="100%">
            <tr>
                <td>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td class="td1">
                                <span class="red">*</span>字段名称：
                            </td>
                            <td>
                                <input id="txt_ColumnName" type="text" style="width: 400px;" />
                            </td>
                        </tr>
                        <tr>
                            <td class="td1">
                                字段描述：
                            </td>
                            <td>
                                <input id="txt_ColumnDescription" type="text" style="width: 400px;" />
                            </td>
                        </tr>
                        <tr>
                            <td class="td1">
                                <span class="red">*</span>字段类型：
                            </td>
                            <td>
                                <div style='position: relative;'>
                                    <span style='margin-left: 120px; width: 22px; overflow: hidden;'>
                                        <select id="ColumnType" onchange="getColumnType(this.value)" style="width: 138px;
                                            margin-left: -120px">
                                        </select>
                                    </span>
                                    <input id="txt_ColumnType" type='text' value="-请选择-" style="width: 115px; position: absolute;
                                        left: 0px;" />
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="td1">
                                是否主键：
                            </td>
                            <td>
                                <input type="checkbox" id="PK" />
                            </td>
                        </tr>
                        <tr>
                            <td valign="top" class="td1">
                                首选字段：
                            </td>
                            <td>
                                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                    <tr>
                                        <td class="addColumn">
                                            <div>
                                                <select id='sel_SourceSysName0' class="sys" onchange="GetViewList(this.value, '0')">
                                                </select>
                                                _<select id='sel_ViewName0' class="view" onchange="GetColList(this.value, '0','')">
                                                </select>
                                                _
                                                <select id='sel_DisplayName0' class="dis">
                                                    <option value=''>-请选择源字段-</option>
                                                </select>
                                                <input type="button" class="button" value="添加" onclick="AddColumn('','','')" />
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</div>
